import Vue from "vue";
import VueRouter from "vue-router";
import * as types from "../store/mutation-types";
import NProgress from "nprogress";

NProgress.configure({ showSpinner: false });

Vue.use(VueRouter);

const routes = [
  {
    path: "/home",
    name: "Index",
    component: () =>
      // import(/* webpackChunkName: "index" */ "../views/code/Home.vue")
      import(/* webpackChunkName: "index" */ "../views/index/Index.vue")
  },
  {
    path: "/login",
    name: "login",
    component: () =>
      import(/* webpackChunkName: "index" */ "../views/login/Login.vue")
    // import(/* webpackChunkName: "index" */ "../views/code/test.vue")
  },
  {
    path: "/",
    name: "grant",
    component: () => import("../views/code/grant.vue")
  },
  {
    path: "/check",
    name: "check",
    component: () => import("../views/code/checkCard.vue")
  },
  {
    path: "/jump",
    name: "jump-page",
    component: () => import("../views/code/jumpPage.vue")
  },
  {
    path: "/bind",
    name: "bind",
    component: () => import("../views/code/bind.vue")
  },
  {
    path: "/success",
    name: "success",
    component: () => import("../views/code/success.vue")
  },
  {
    path: "/test/show",
    name: "Show",
    component: () =>
      import(/* webpackChunkName: "test" */ "../views/test/Index.vue")
  },
  {
    path: "/test/apply",
    name: "Apply",
    component: () =>
      import(/* webpackChunkName: "test" */ "../views/test/Apply.vue")
  },
  {
    path: "/test/cashier",
    name: "Cashier",
    component: () =>
      import(/* webpackChunkName: "cashier" */ "../views/test/Cashier.vue")
  },
  {
    path: "/test/face",
    name: "Face",
    component: () =>
      import(/* webpackChunkName: "cashier" */ "../views/test/Face.vue")
  },
  {
    path: "/test/result",
    name: "Result",
    component: () =>
      import(/* webpackChunkName: "cashier" */ "../views/test/result.vue")
  },
  {
    path: "/code",
    name: "Code",
    component: () =>
      // import(/* webpackChunkName: "code" */ "../views/code/Home.vue")
      import(/* webpackChunkName: "code" */ "../views/code/HomeFormal.vue")
  },
  {
    path: "/building",
    name: "building",
    component: () => import("../views/code/build.vue")
  },
  {
    path: "/code/mine",
    name: "CodeMine",
    component: () =>
      import(/* webpackChunkName: "code-mine" */ "../views/code/Mine.vue")
  },
  {
    path: "/code/mine/card",
    name: "CodeMineCard",
    // meta: { name: "一码通 - 卡包" },
    component: () =>
      import(/* webpackChunkName: "code-mine-card" */ "../views/code/Card.vue")
  },
  {
    path: "/code/mine/record",
    name: "CodeMineRecord",
    // meta: { name: "一码通 - 记录" },
    component: () =>
      import(
        /* webpackChunkName: "code-mine-record" */ "../views/code/Record.vue"
      )
  },
  {
    path: "/code/mine/notice",
    name: "CodeMineNotice",
    // meta: { name: "一码通 - 消息" },
    component: () =>
      import(
        /* webpackChunkName: "code-mine-notice" */ "../views/code/Notice.vue"
      )
  },
  {
    path: "/code/mine/notice/detail",
    name: "noticeDetail",
    component: () => import("../views/code/noticeDetail.vue")
  },
  {
    path: "/code/mine/share",
    name: "CodeMineShare",
    component: () =>
      import(
        /* webpackChunkName: "code-mine-share" */ "../views/code/Share.vue"
      )
  },
  {
    path: "/code/mine/setting",
    name: "CodeMineSetting",
    // meta: { name: "一码通 - 设置" },
    component: () =>
      import(
        /* webpackChunkName: "code-mine-setting" */ "../views/code/Setting.vue"
      )
  },
  {
    path: "/code/mine/logout",
    name: "CodeMineLogout",
    component: () => import("../views/code/logout.vue")
  },
  {
    path: "/common/popup",
    name: "popup",
    component: () => import("../components/popup/popup.vue")
  },
  {
    path: "/inner",
    name: "inner",
    component: () => import("../views/code/inner.vue")
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  if (to.meta.name) {
    document.title = to.meta.name;
  }
  next();
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const token = Vue.storage.get(types.ACCESS_TOKEN);
    if (token) {
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
